package com.shaoyu.spring.study.validate.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;

/**
 * @Desc :
 * @Author: shaoyu
 * @Date : 2022/8/18 17:24
 */
public class BitOperationUtil {

    public static final long ALL_TRUE = ~0L;
    public static final long ALL_FALSE = 0L;

    private static void checkIndex(int index){
        if(index < 0 || index > 63){
            throw new IndexOutOfBoundsException("下标位index范围为[0-63]");
        }
    }
    public static long setTrue(long origin,int ... index){
        if(index == null || index.length == 0){
            return origin;
        }
        for(int i : index){
            checkIndex(i);
            origin |= 1L<<i;
        }
        return origin;
    }
    public static long setFalse(long origin,int ... index){
        if(index == null || index.length == 0){
            return origin;
        }
        for(int i : index){
            checkIndex(i);
            origin &= (~(1L<<i));
        }
        return origin;
    }

    public static boolean isTrue(long origin,int index){
        checkIndex(index);
        int num = 1 << index;
        return (origin & num) == num;
    }
    public static boolean isFalse(long origin,int index){
        return !isTrue(origin, index);
    }

    public static void main(String[] args) {

        Arrays.asList(8554645781634L,138555753540L,153571392580L,138589323304L,8522433526800L)
                .forEach(l->System.out.println(new StringBuilder(Long.toBinaryString(l).replaceAll("1", "█").replaceAll("0", " ")).reverse()));
    }
}
